/*
OpenMVL Matrix Vector Library
Copyright (c) 2009 FluidInteractive (R)
 
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising
from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
 
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
 
Written by: Nicola Candussi <nicola@fluidinteractive.com>
*/

//expr_cmat.hpp

#ifndef MVL_EXPR_CMAT_HPP
#define MVL_EXPR_CMAT_HPP

#include "mat_mat_assign.hpp"
#include "type_traits.hpp"

namespace mvl {

template<typename L, typename T>
class expr_cmat : public expr<expr_cmat<L, T> > 
{ 
public:
    typedef expr_cmat self_t;

public:
    typedef L                               layout_t;
    typedef T                               value_type;
    typedef typename layout_t::order        order;
    typedef typename layout_t::Rows         Rows;
    typedef typename layout_t::Cols         Cols;
    typedef typename layout_t::Size         Size;

    typedef self_t closure_t;
    typedef const self_t const_closure_t;

public:
    expr_cmat(T* data) : m_data(data) { }

    self_t& operator = (self_t const& rhs) {
        mat_mat_assign<self_t, self_t, fn_assign<value_type, value_type> >::apply(*this, rhs);
        return *this;
    }

    template<typename E2>
    self_t& operator = (expr<E2> const& rhs) {
        MVL_STATIC_CHECK((E2::Rows::value == Rows::value), "mat_size_mismatch");
        MVL_STATIC_CHECK((E2::Cols::value == Cols::value), "mat_size_mismatch");
        mat_mat_assign<self_t, E2, fn_assign<value_type, value_type> >::apply(*this, rhs());
        return *this;
    }

private:

    template<typename Entry>
    struct sparse_index : Entry::index {};

    template<typename Entry>
    struct dense_index {
        MVL_STATIC_CHECK((meta::less<typename Entry::i, Rows>::value), tml_index_out_of_bound);
        MVL_STATIC_CHECK((meta::less<typename Entry::j, Cols>::value), tml_index_out_of_bound);
        typedef typename meta::if_<
            typename meta::is_same<
                typename L::order, 
                layout::row_major_tag
            >::type,
            meta::int_<Entry::i::value * Cols::value + Entry::j::value>,
            meta::int_<Entry::j::value * Rows::value + Entry::i::value>
        >::type type;
    };

    template<typename Entry>
    struct get_index {
        MVL_STATIC_CHECK((meta::is_same<typename Entry::tag, layout::_v_>::value), tml_entry_not_a_value);
        typedef typename meta::eval_if<
            typename meta::is_same<
                typename L::tag,
                layout::sparse_layout_tag
            >::type,
            sparse_index<Entry>,
            dense_index<Entry>
        >::type type;

        MVL_STATIC_CHECK((meta::greater_equal<type, meta::int_<0> >::value), tml_index_out_of_bound);
        MVL_STATIC_CHECK((meta::less<type, Size >::value), tml_index_out_of_bound);
    };

public:

    //access
    template<typename Entry>
    T at() const {
        typedef typename get_index<Entry>::type index;
        return m_data[index::value];
    }

    template<typename Entry>
    T& at() {
        typedef typename get_index<Entry>::type index;
        return m_data[index::value];
    }

private:
    T* m_data;
};

template<typename L, typename T>
struct cmat_traits 
{
    typedef expr_cmat<L, T> type;
};

template<typename L, typename T>
inline 
const typename cmat_traits<L, const T>::type
cmat(T const* data) {
    typedef const typename cmat_traits<L, const T>::type expr_t;
    return expr_t(data);
}

template<typename L, typename T>
inline 
typename cmat_traits<L, T>::type 
cmat(T* data) {
    typedef typename cmat_traits<L, T>::type expr_t;
    return expr_t(data);
}

} //namespace mvl

#endif

